﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Web.Security;
using Acms.Bll;

namespace Acms.AcmsControls.ContentControls.FrontEnd
{
    public class AcmsAdminControl : WebControl
    {
        protected override void CreateChildControls()
        {
            RequiresDataBinding = true;

            this.Controls.Add(new LiteralControl("ACMS"));
            
            menu = new RadMenu();
            menu.ID = "menu";
            this.Controls.Add(menu);
        }

        RadMenu menu;

        protected bool RequiresDataBinding { get; private set; }

        protected override void OnPreRender(EventArgs e)
        {
            if (RequiresDataBinding)
            {
                DataBind();
            }
            base.OnPreRender(e);
        }

        public override void DataBind()
        {
            EnsureChildControls();
            menu.Items.Clear();
            TemplatePage acmsPage = Page as TemplatePage;

            if (acmsPage != null)
            {
                /*RadMenuItem armfelt = new RadMenuItem();
                armfelt.Text = "Acms";
                armfelt.NavigateUrl = "http://www.armfelt.co.uk";
                armfelt.Target = "_Blank";
                menu.Items.Add(armfelt);
                */

                if(acmsPage.User != null && acmsPage.User.HasAdminRights())
                {
                    RadMenuItem version = new RadMenuItem();
                    if(acmsPage.AcmsContext.SiteVersion == null)
                    {
                        version.Text = "Pending Version";
                    }
                    else if(acmsPage.AcmsContext.SiteVersion <= DateTime.Now)
                    {
                        version.Text = "Live Version";
                    }
                    else
                    {
                        version.Text = string.Format("{0:dd MMM yyyy HH:mm} Version",  acmsPage.AcmsContext.SiteVersion);
                    }
                    menu.Items.Add(version);

                    List<Post> versions = Post.GetCurrentVersions(acmsPage.AcmsContext.SiteId, acmsPage.AcmsContext.CurrentPost.PostGuid);  
                    if (versions.Count > 0)
                    {
                        RadMenuItem selectVersion = new RadMenuItem();
                        selectVersion.Text = "Select Version";
                        version.Items.Add(selectVersion);
                        foreach (Post postVersion in versions)
                        {
                            if (postVersion.Id != acmsPage.AcmsContext.PostId)
                            {
                                RadMenuItem tmpVersion = new RadMenuItem();
                                if (postVersion.SiteVersion == null)
                                {
                                    tmpVersion.Text = "Pending";
                                }
                                else if (postVersion.SiteVersion.Value < DateTime.Now)
                                {
                                    tmpVersion.Text = "Live";
                                }
                                else
                                {
                                    tmpVersion.Text = postVersion.SiteVersion.Value.ToString("dd MMM yyyy HH:mm");
                                }
                                tmpVersion.NavigateUrl = postVersion.Url;
                                selectVersion.Items.Add(tmpVersion);
                            }
                        }
                    }

                    foreach(Post p in acmsPage.GetRegisteredPosts().Keys)
                    {
                        RadMenuItem postItem = new RadMenuItem();
                        postItem.Text = string.Concat(p.DisplayName, " (",  p.Path, ")");
                        version.Items.Add(postItem);

                        foreach (PostControl ctrl in acmsPage.GetRegisteredPosts()[p])
                        {
                            RadMenuItem controlItem = new RadMenuItem();
                            controlItem.Text = ctrl.Name;
                            postItem.Items.Add(controlItem);
                        }
                    }
                }
                else if(acmsPage.User != null)
                {
                    RadMenuItem login = new RadMenuItem();
                    login.Text = "Login";
                    if (!string.IsNullOrEmpty(FormsAuthentication.LoginUrl))
                    {
                        login.NavigateUrl = FormsAuthentication.LoginUrl;
                    }
                    else
                    {

                        login.NavigateUrl = Page.ResolveUrl("~/Acms/Logon.aspx");
                    }
                    menu.Items.Add(login);
                }

            }
        }
    }
}
